/**
 * Project Name:九色鹿
 * File Name:AutoNumProvider.java
 * Package Name:com.fxp.project.express.provider
 * Date:2021年09月14日 14:22
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.express.provider;

import com.alibaba.fastjson.JSONArray;
import com.fxp.project.express.config.KuaiDiAutoNumProperties;
import com.fxp.project.express.constant.Constants;
import com.fxp.project.express.entity.request.AutoNumReq;
import com.fxp.project.express.entity.request.BaseRequest;
import com.fxp.project.express.entity.response.AutoNumResp;
import com.fxp.project.express.utils.ObjectToMapUtils;
import com.fxp.project.jiuselu.commons.http.HttpRestTools;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:AutoNumProvider<br/>
 * Function: 实际方法对象<br/>
 * Date:2021年09月14日 14:22<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK 8
 */
@Log4j2
public class AutoNumProvider {

    /**
     * 配置对象
     */
    private KuaiDiAutoNumProperties properties;

    /**
     * AutoNumProvider: 私有的构建方法<br/>
     *
     * @param properties
     * @author Jason.Wang
     * @date 2021/9/13 17:49
     */
    private AutoNumProvider(KuaiDiAutoNumProperties properties) {
        this.properties = properties;
    }

    /**
     * init: 初始化方法<br/>
     *
     * @param properties
     * @return
     * @author Jason.Wang
     * @date 2021/9/13 17:50
     */
    public AutoNumProvider init(KuaiDiAutoNumProperties properties) {
        this.properties = properties;
        return new AutoNumProvider(properties);
    }

    /**
     * getApiUrl: 构建请求URL对象<br/>
     *
     * @param request
     * @return
     * @author Jason.Wang
     * @date 2021/9/14 14:24
     */
    public String getApiUrl(BaseRequest request) {
        AutoNumReq autoNumReq = (AutoNumReq) request;
        return String.format(Constants.AUTO_NUM_URL, autoNumReq.getNum(), properties.getKey());
    }

    /**
     * 根据单号获取快递公司列表
     *
     * @return
     */
    public List<AutoNumResp> getComByNumList(BaseRequest request) throws Exception {
        String json = HttpRestTools.postForEntityForm(getApiUrl(request), ObjectToMapUtils.objectToMap(request), String.class);
        return JSONArray.parseArray(json, AutoNumResp.class);
    }
}